//
//  XTNewFeatureCollectionViewCell.swift
//  HrenryWeibo
//
//  Created by 熊涛 on 16/5/14.
//  Copyright © 2016年 熊涛. All rights reserved.
//

import UIKit

class XTNewFeatureCollectionViewCell: UICollectionViewCell {
    
    
    //定义一个闭包
    var callBack : (() -> ())?
    
    
    var index = 0{
        //属性值已经改变了
        didSet {
            XTPrint(items: "index == \(index)")
            imageView.image = UIImage(named: "new_feature_" + "\(index + 1)")
            startBtn.hidden = true
        }
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
       //添加,设置子控件
        subViewLaout()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加,设置子控件
   private func subViewLaout() {
        
        //添加子控件
         self.contentView.addSubview(imageView)
        self.contentView.addSubview(startBtn)
        
        //设置约束
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.contentView)
        }
    
        startBtn.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.contentView)
            make.bottom.equalTo(self.contentView).offset(-160)
        }
        
    }
    
    //显示按钮startBtn
    func showStartBtnAndAnimation() {
    
        startBtn.hidden = false
        //将按钮的大小设置成0
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(0.5, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 20, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformMakeScale(1, 1)
            }) { (_) -> Void in
                XTPrint(items: "按钮动画完成")
        }
        
    }
    
    
    //MARK: - 按钮点击事件
   @objc private func startBtnClick() {
    
        //切换跟控制器
        callBack?()
    }
    
    
    //MARK: - 懒加载
    //图片
    private lazy var imageView : UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    //按钮
    lazy var startBtn : UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        
        //设置
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(16)
        
        //自适应大小
        button.sizeToFit()
        
        //隐藏
        button.hidden = true
        
        //给button添加响应事件
        button.addTarget(self, action: "startBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
}
